package com.seanzx.boot;

import com.seanzx.dp.ProcessorController;
import com.seanzx.dp.common.DBProperties;
import com.seanzx.dp.common.MetaTables;
import com.seanzx.dp.entity.Column;
import com.seanzx.dp.entity.Table;

import java.io.IOException;

public class BootLoader {
    private final ProcessorController processorController = ProcessorController.get();

    public void load() {
        DBProperties.init();
        try {
            initTableSchema();
        } catch (IOException e) {
            System.out.println("init table schema failed");
        }
    }

    private void initTableSchema() throws IOException {
        System.out.println("=====init table schema start=====");
        if (!processorController.showDatabase().getData().isEmpty()) {
            System.out.println("=====init table schema end=====");
            return;
        }
        processorController.createDatabase("information_schema");
        Table tables = MetaTables.tables();
        Table columns = MetaTables.columns();

        processorController.getTableProcessor().create(tables.getTableSchema(), tables.getName());
        processorController.getTableProcessor().create(columns.getTableSchema(), columns.getName());

        processorController.insertTableConfiguration(tables, tables.getColumns());
        processorController.insertTableConfiguration(columns, tables.getColumns());

        for (Column tableColumn : tables.getColumns()) {
            processorController.insertColumnConfiguration(tableColumn, columns.getColumns());
        }
        for (Column columnColumn : columns.getColumns()) {
            processorController.insertColumnConfiguration(columnColumn, columns.getColumns());
        }
        System.out.println("=====init table schema end=====");
    }

    public static void main(String[] args) {
        new BootLoader().load();
    }
}
